function IntegralProgrammeEdit() {
    var _this = this;

    _this.init = function () {
        $(".nav").hide();
        $("#startTime").val(TimeUtil.longMsTimeConvertToDateTime($("#startTime").val()));
        $("#endTime").val(TimeUtil.longMsTimeConvertToDateTime($("#endTime").val()));

        _this.initRules();
        _this.loadEditable();
        _this.handle();
    };

    // 选项内容增加删除一项
    _this.optionsAddOrDel = function(funcName) {
        $("#addOrDelOptions tr").each(function () {
            var obj = $(this);
            if(funcName == 'add'){
                //添加一项
                var objDisplay = $(obj).css("display");
                if(objDisplay == "none"){
                    obj.css("display","block");
                    return false;
                }
            }else{
                //删除一项
                var objDisplay = $(obj).next().css("display");
                if(objDisplay != "block"){
                    obj.css("display","none");
                    return false;
                }
            }
        });
    };

    _this.initRules = function () {
        //登录
        var integralRule7 = $("#integralRule7").val();
        if(integralRule7!=''){
            var json = eval("("+integralRule7+")");
            $("#nextDay7").attr("data-value",json.nextDay);
            var arr = json.loginRule;
            $("#addOrDelOptions tr").each(function (i) {
                var obj = $(this);
                if(i<arr.length-1){
                    obj.css("display","block");
                }
            });
            for(var i=0;i<arr.length;i++){
                var arrDay = arr[i].day;
                var arrScore = arr[i].score;
                if(arrDay == "1"){
                    $("#score7").attr("data-value",arrScore);
                } else {
                    var array = arrDay.split("-");
                    $("#day7_"+i+"_1").attr("data-value",array[0]);
                    $("#day7_"+i+"_2").attr("data-value",array[1]);
                    $("#score7_"+i).attr("data-value",arrScore);
                }
            }
        }
        //考试积分
        var integralRule8 = $("#integralRule8").val();
        if(integralRule8!=''){
            var json = eval("("+integralRule8+")");
            $("#score8").attr("data-value",json.score);
            if(json.dayLimit=='1'){
                $("input[name='dayLimit8'][value='1']").attr("checked","checked");
                $("#label_day_limit_score8").hide();
            }else {
                $("input[name='dayLimit8'][value='2']").attr("checked","checked");
                $("#dayLimitScore8").attr("data-value",json.dayLimitScore);
                $("#label_day_limit_score8").show();
            }
        }
        //学时积分
        var integralRule9 = $("#integralRule9").val();
        if(integralRule9!=''){
            var json = eval("("+integralRule9+")");
            $("#studytime9").attr("data-value",json.studytime);
            $("#score9").attr("data-value",json.score);
            $("#studytimeMin9").attr("data-value",json.studytimeMin);
            if(json.dayLimit=='1'){
                $("input[name='dayLimit9'][value='1']").attr("checked","checked");
                $("#label_day_limit_score9").hide();
            }else {
                $("input[name='dayLimit9'][value='2']").attr("checked","checked");
                $("#dayLimitScore9").attr("data-value",json.dayLimitScore);
                $("#label_day_limit_score9").show();
            }
        }
        //自学积分
        var integralRule10 = $("#integralRule10").val();
        if(integralRule10!=''){
            var json = eval("("+integralRule10+")");
            $("#studytime10").attr("data-value",json.studytime);
            $("#score10").attr("data-value",json.score);
            $("#studytimeMin10").attr("data-value",json.studytimeMin);
            if(json.dayLimit=='1'){
                $("input[name='dayLimit10'][value='1']").attr("checked","checked");
                $("#label_day_limit_score10").hide();
            }else {
                $("input[name='dayLimit10'][value='2']").attr("checked","checked");
                $("#dayLimitScore10").attr("data-value",json.dayLimitScore);
                $("#label_day_limit_score10").show();
            }
        }

        //每日一练
        var integralRule16 = $("#integralRule16").val();
        if(integralRule16!=''){
            var json = eval("("+integralRule16+")");
            $("#score16").attr("data-value",json.score);
            /*if(json.dayLimit=='1'){
                 $("input[name='dayLimit16'][value='1']").attr("checked","checked");
            }else {
                $("input[name='dayLimit16'][value='2']").attr("checked","checked");
            }*/
        }

    };

    //初始化插件
    _this.loadEditable = function () {
        $("form[class^=form_integral_rule]").find("input[type=text]").each(function () {
            $(this).editable({
                validate:function (value) {
                    if(value != ""){
                        if(isNaN(value) || value == 0){
                            return  '输入不合法';
                        }
                        if(Number(value) < 0){
                            return  '不能输入负数';
                        }
                        if(Number(value) != parseInt(value)){
                            return  '不能输入小数';
                        }
                        var max = $(this).attr("data-max");
                        var min = $(this).attr("data-min");
                        if(Number(value) > Number(max)){
                            return  '最大值为' + max;
                        }
                        if(Number(value) < Number(min)){
                            return  '最小值为' + min;
                        }
                    }
                },
                display:function (value) {
                    $(this).val(value);
                    $(this).trigger("keyup");
                }
            })
        });
    };

    _this.validate = function () {
        var varName = $.trim($("#varName").val());
        if ("" == varName) {
            layer.msg("请填写方案名称");
            return false;
        }
        var introduction = $.trim($("#introduction").val());
        if ("" == introduction) {
            layer.msg("请填写方案简介");
            return false;
        }
        var startTime = $.trim($("#startTime").val());
        if ("" == startTime) {
            layer.msg("请填写生效时间");
            return false;
        }
        var endTime = $.trim($("#endTime").val());
        if ("" == endTime) {
            layer.msg("请填写到期时间");
            return false;
        }
    };

    _this.handle = function () {
        $("#save").click(function () {
            if(false == _this.validate()){
                return;
            }

            _this.save7();
            _this.save8();
            _this.save9();
            _this.save10();
            _this.save16();

            $.ajax({
                url: appPath + "/admin/integralProgramme/save",
                dataType: "json",
                async: true,
                type: "POST",
                data: $("#integral_programme_form").serializeArray(),
                success:function(data){
                    var result = data.code;
                    if (10000 == result) {
                        layer.alert('操作成功', {icon : 1, closeBtn:0},function (index) {
                            layer.close(index);
                            window.location.href = appPath + '/admin/integralProgramme/index';
                        });
                    } else {
                        layer.alert('操作失敗', {icon : 2});
                    }
                }
            });
        });
    }


    //登录
    _this.save7 = function () {
        if( $("#score7").val()!='') {
            var loginRule = [{"day": "1", "score": $("#score7").val()}];
            if ($("#day7_1_1").val() != '') {
                loginRule.push({
                    "day": $("#day7_1_1").val() + "-" + $("#day7_1_2").val(),
                    "score": $("#score7_1").val()
                });
            }
            if ($("#day7_2_1").val() != '') {
                loginRule.push({
                    "day": $("#day7_2_1").val() + "-" + $("#day7_2_2").val(),
                    "score": $("#score7_2").val()
                });
            }
            if ($("#day7_3_1").val() != '') {
                loginRule.push({
                    "day": $("#day7_3_1").val() + "-" + $("#day7_3_2").val(),
                    "score": $("#score7_3").val()
                });
            }
            if ($("#day7_4_1").val() != '') {
                loginRule.push({
                    "day": $("#day7_4_1").val() + "-" + $("#day7_4_2").val(),
                    "score": $("#score7_4").val()
                });
            }

            var m = {
                "integralType": $("#integralType7").val(),
                "loginReq": $("#loginReq7").val(),
                "loginRule": loginRule,
                "nextDay": $("#nextDay7").val()
            };
            $("#integralRule7").val(JSON.stringify(m));
        }else {
            $("#integralRule7").val('');
        }
    }

    //考试积分
    _this.save8 = function () {
        if ($("#score8").val() != '') {
            var m = {
                "integralType": $("#integralType8").val(),
                "score": $("#score8").val(),
                "dayLimit": $("input[name='dayLimit8']:checked").val(),
                "dayLimitScore": $("#dayLimitScore8").val()
            };
            $("#integralRule8").val(JSON.stringify(m));
        }else {
            $("#integralRule8").val('');
        }
    }

    //学时积分
    _this.save9 = function () {
        if ($("#score9").val()!='' && $("#studytime9").val()!='') {
            var m = {
                "integralType": $("#integralType9").val(),
                "studytime": $("#studytime9").val(),
                "score": $("#score9").val(),
                "dayLimit": $("input[name='dayLimit9']:checked").val(),
                "dayLimitScore": $("#dayLimitScore9").val(),
                "studytimeMin": $("#studytimeMin9").val()
            };
            $("#integralRule9").val(JSON.stringify(m));
        }else{
            $("#integralRule9").val('');
        }
    }

    //自学积分
    _this.save10 = function () {
        if ($("#studytime10").val()!='' && $("#score10").val()!='') {
            var m = {
                "integralType": $("#integralType10").val(),
                "studytime": $("#studytime10").val(),
                "score": $("#score10").val(),
                "dayLimit": $("input[name='dayLimit10']:checked").val(),
                "dayLimitScore": $("#dayLimitScore10").val(),
                "studytimeMin": $("#studytimeMin10").val()
            };
            $("#integralRule10").val(JSON.stringify(m));
        }else {
            $("#integralRule10").val('');
        }
    }


    //每日一练
    _this.save16 = function () {
        if ($("#score16").val() != '') {
            var m = {
                "integralType": $("#integralType16").val(),
                "score": $("#score16").val()
                // "dayLimit": $("input[name='dayLimit16']:checked").val()
            };
            $("#integralRule16").val(JSON.stringify(m));
        }else {
            $("#integralRule16").val('');
        }
    }


    _this.doDayLimit8 = function (_value) {
        if("1"==_value){
            $("#label_day_limit_score8").hide();
            $("#dayLimitScore8").val('');
        } else {
            $("#label_day_limit_score8").show();
        }
    }

    _this.doDayLimit9 = function (_value) {
        if("1"==_value){
            $("#label_day_limit_score9").hide();
            $("#dayLimitScore9").val('');
        } else {
            $("#label_day_limit_score9").show();
        }
    }

    _this.doDayLimit10 = function (_value) {
        if("1"==_value){
            $("#label_day_limit_score10").hide();
            $("#dayLimitScore10").val('');
        } else {
            $("#label_day_limit_score10").show();
        }
    }

    _this.doDayLimit16 = function (_value) {
        if("1"==_value){
            $("#label_day_limit_score16").hide();
            $("#dayLimitScore16").val('');
        } else {
            $("#label_day_limit_score16").show();
        }
    }

}

var integralProgrammeEdit = new IntegralProgrammeEdit();